<template>
  <div>
    <el-form ref="form" :model="form" label-width="100px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择部门负责人" style="width:100%">
          <el-option v-for="item in employees" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="deptSubmit">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeeSimple, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    // 父组件传递的精简数据
    originList: {
      type: Array,
      required: true
    },
    parentId: {
      type: [Number, String],
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    }
  },

  data() {
    // 验证编码：编码在整个模块不能重复
    const validCode = (rule, value, callback) => {
      // 需要code组成一个数组
      let existCodeList = this.originList.map(item => item.code)
      if (this.isEdit) {
        // 点击编辑时部门编码出现重复优化
        existCodeList = this.originList.filter(item => item.id !== this.parentId).map(item => item.code)
      }

      existCodeList.includes(value) ? callback(new Error(`部门编码${value}已经存在`)) : callback()
    }
    // 验证部门名称
    const validNmae = (rule, value, callback) => {
      // 先找到同级部门，再把同级部门的name组成一个数组
      const exitNameList = this.originList.filter(item => item.id === this.parentId).map(item => item.name)

      // 使用includes方法进行对比
      exitNameList.includes(value) ? callback(new Error(`同级部门名称不能重复`)) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validNmae }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    //   调用获取负责人方法
    this.getEmployeeSimple()

    // 调用获取部门详情方法
    this.getDepartDetail()
  },

  methods: {
    //   获取部门详情
    async getDepartDetail() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.parentId)
        // console.log(res)
        this.form = res.data
      }
    },

    //   获取负责人列表
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      this.employees = res.data
    },

    // 新增组织架构方法
    async addDepars() {
      const res = await addDepartments({ ...this.form, pid: this.parentId })
      // 给用户提示
      this.$message.success(res.message)

      // 父级渲染列表并关闭弹窗
      this.$emit('update-list')
    },

    // 编辑组织架构方法
    async editDeparts() {
      const res = await updateDepartments(this.form)

      //   给用户提示
      this.$message.success(res.message)

      // 父级渲染列表并关闭弹窗
      this.$emit('update-list')
    },
    // 点击确定
    async deptSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.editDeparts() : this.addDepars()
        }
      })
    },

    // 取消事件
    cancel() {
      this.$emit('close')
    }
  }
}
</script>

<style>
form.el-form {
    padding: 0 60px 0 30px;
}
</style>
